package com.ms.log.level;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zdh
 * 日志级别
 */
public class Level implements Serializable {


    public static final Level ERROR = new Level("WARN", 1000);
    public static final Level WARN = new Level("WARN", 900);
    public static final Level INFO = new Level("INFO", 800);
    public static final Level DEBUG = new Level("DEBUG", 700);
    public static final Level TRACE = new Level("TRACE", 600);

    public static final Map<String,Level> levelMap ;
    static {

        Map<String,Level> baseMap = new HashMap<>();
        baseMap.put("DEBUG",DEBUG);
        baseMap.put("debug",DEBUG);
        baseMap.put("TRACE",TRACE);
        baseMap.put("trace",TRACE);
        baseMap.put("INFO",INFO);
        baseMap.put("info",INFO);
        baseMap.put("WARN",WARN);
        baseMap.put("warn",WARN);
        baseMap.put("ERROR",ERROR);
        baseMap.put("error",ERROR);
        levelMap = Collections.unmodifiableMap(baseMap);
    }

    public static Level getLevel(String levelName){
        Level level = levelMap.get(levelName);
        return level!=null ? level:INFO;
    }

    /**
     * 级别名称
     */
    private final String name;
    /**
     * 级别编号
     */
    private final int value;

    public Level(String name, int value) {
        this.name = name;
        this.value = value;
    }


    public String getName() {
        return name;
    }

    public int getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "Level(" + name +    ", " + value + ")";
    }
}
